#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"

void stack_full(ST* ps)
{
	if (ps->top == ps->capacity)
	{
		ps->capacity += 2;
		ST_DAYE_TYPE* b = realloc(ps->a,sizeof(ST_DAYE_TYPE) * ps->capacity);
		if (b)
		{
			ps->a = b;
			printf("capacity increase right\n");
		}
		else
			printf("capacity increase error\n");
	}
}

void stack_init(ST* ps)
{
	ps->a = malloc(sizeof(ST_DAYE_TYPE) * 4);
	ps->capacity = 4;
	ps->top = 0;
}

void stack_destroy(ST* ps)
{
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void stack_push(ST* ps , ST_DAYE_TYPE x)
{
	stack_full(ps);
	ps->a[ps->top] = x;
	ps->top++;
}

void stack_pop(ST* ps)
{
	ps->top--;
}

void stack_print(ST ps)
{
	printf("stack top\n");
	for (int i = ps.top - 1 ; i >= 0; i--)
	{
		printf("%d\n", ps.a[i]);
	}
	printf("stack bottom\n");
}

ST_DAYE_TYPE stack_top(ST* ps)
{
	return ps->a[ps->top - 1];
}

int stack_size(ST* ps)
{
	return ps->top;
}

int stack_empty(ST* ps)
{
	if (ps->top == 0)
		return 1;
	else
		return 0;
}